<?php
/**
 * User: b179a9
 * Date: 9/29/11
 * Time: 5:51 PM
 */

use Managers\Text\Filter\StopWordFilter;

class CommentsController extends G4T_Controller
{
    /**
     * @var \Managers\Comments
     */
    protected $manager;

    protected $user;

    function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
            $response = $this->getResponse();
            $response->setHeader('Content-Type', 'text/x-json');
        } else {
            $this->_helper->layout()->setLayout('index-layout');
        }

        $this->user = \Zend_Auth::getInstance()->getStorage()->read();
        if (!$this->user) {
            $this->_forward('denied');
            return;
        }
        $this->manager = new \Managers\Comments();
    }

    public function deniedAction()
    {

    }

    function viewAction()
    {
        $article = $this->_request->getParam('news');
        $commentsManager = new \Managers\Comments();
        $comments = $commentsManager->getTreeList($article);
        $this->view->comments = $comments;
        $this->view->article = $article;

    }

    public function editAction()
    {
        $form = $this->getCommentFormBuilder()->addField('id', 'hidden')->getForm();
        $comment = $this->manager->getById($this->_getParam('id'));
        if (!$this->hasAccess($comment)) {
            die('Todo');
        }
        $form->populate($comment);
        if ($this->_request->isPost()) {
            if ($form->isValid($this->_getAllParams())) {

            }
        }
    }

    public function addAction()
    {
        $form = $this->getCommentFormBuilder()->getForm();
        $newsId = $this->_getParam('news');

        $newsManager = new \Managers\News();
        if ($newsManager->allowComments($newsId)) {
            if ($this->_request->isPost()) {
                if ($form->isValid($this->_getAllParams())) {
                    $values = $form->getValues();

                    $filter = new StopWordFilter();
                    foreach ($values as &$value) {
                        $value = $filter->sanitize($value);
                    }

                    if (!$filter->hasErrors()) {
                        $this->manager->create($values, $this->user);
                        $this->renderScript('/ajax/success.phtml');
                    } else {
                        $form->populate($values);
                        $foundWords = $filter->getFoundWords();
                        $this->view->message = sprintf("Слова: %s - недопустимы.", join(',', $foundWords));
                    }
                }
            }
            $this->view->form = $form;
        }
    }

    public function deleteAction()
    {

    }

    public function formAction()
    {
        $form = $this->getCommentFormBuilder()->getForm();
        $form->populate(array('news' => $this->_getParam('news'), 'parent' => $this->_getParam('parent')));
        $this->view->form = $form;
    }

    private function getCommentFormBuilder()
    {
        $formBuilder = \G4T\Form\FormBuilder::createFormBuilder(\Managers\Comments::TABLE,
            array(
                'action' => '/comments/add',
                'class' => 'comment-add-form'
            )
        )
//            ->addField('email', 'email', array('label' => 'Email'))
            ->addField('text', 'text', array('label' => 'Text'))
//            ->addField('token', 'hash')
            ->addField('news', 'hidden')
            ->addField('parent', 'hidden', array('required' => false))
            ->addField('submit', 'submit', array('label' => 'Save'));
        return $formBuilder;
    }

    private function hasAccess($comment)
    {
        return $comment && $comment['user']['id'] == $this->user['id'];
    }

    public function rateAction()
    {
        $ratingManager = new \Managers\CommentsRating();
        $result = $ratingManager->rate($this->_getParam('comment'), $this->_getParam('value'));
        $rating = $ratingManager->getRating($this->_getParam('comment'));
        print_r($rating);
        die;
        $this->view->data = $rating;
        $this->renderScript('/ajax/json.phtml');
    }

}
